<?php

namespace app\admin\controller\pay;

use app\common\controller\Backend;

class Order extends Backend
{
    
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\PayOrder;
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("typeList", $this->model->getTypeList());

        $time2 = date("Y-m-d",strtotime("-2 day")).' 23:59:59';
        $time1 = date("Y-m-d",strtotime("-30 day")).' 23:59:59';
        $this->CommonPayOrderModel->where(['status'=>0])->where('createtime','between time',[$time1,$time2])->delete();
    }
	public function qzbd(){
        $id = $this->request->param('id');
        $res = $this->model->where(['id'=>$id])->find();
        if (!$res) $this->error('订单不存在');
        $url = orderEnd($id);
        return json(['code'=>1,'msg'=>'OK','data'=>$url['url']]);
    }
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $list = collection($list)->toArray();
            foreach ($list as $k => &$v) {
                $v['bd'] = false;
                $user = model('User')->where(['id'=>$v['uid']])->find();
                if ($user && $v['status'] == 1) {
                    $array=[
                        'pid'=>$user['api_id'],
                        'trade_no'=>$v['trade_no'],
                        'out_trade_no'=>$v['out_trade_no'],
                        'type'=>$v['type'],
                        'name'=>$v['title'],
                        'money'=>round($v['money'],2),
                        'trade_status'=>'TRADE_SUCCESS'
                    ];
                    $arg = argSort(paraFilter($array));
                    $prestr = createLinkstring($arg);
                    $urlstr = createLinkstringUrlencode($arg);
                    $sign = md5Sign($prestr, $user['api_key']);
                    $n = strpos($v['return_url'],'?') ? '&' : '?';
                    $url = $v['return_url'].$n.$urlstr.'&sign='.$sign.'&sign_type=MD5';
                    $v['bd'] = $url;
                }
            }
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if ($ids) {
            $pk = $this->model->getPk();
            $adminIds = $this->getDataLimitAdminIds();
            if (is_array($adminIds)) {
                $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
            }
            $list = $this->model->where($pk, 'in', $ids)->select();
            $count = 0;
            foreach ($list as $k => $v) {
                if ($v['status'] == 1) {
                    $user = model('User')->where(['id'=>$v['uid']])->find();
                    $money = $user['money']-$v['realmoney'];
                    model('User')->where(['id'=>$v['uid']])->update(['money'=>$money]);
                }
                $count += $v->delete();
            }
            if ($count) {
                $this->success();
            } else {
                $this->error(__('No rows were deleted'));
            }
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }
}
